﻿using System;
using System.Configuration;
using ServiceStack;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using System.Security;
using System.Collections.Generic;
using ServiceStack.FluentValidation.Results;
using GameTools;

namespace PuSuoPai
{
	public class PuSuoPaiService : Service
	{
		//================================= Login =================================
		public Player Post(LoginRqt request)
		{
			Player player = null;

			try {
				if (string.IsNullOrEmpty (request.Name) || string.IsNullOrEmpty (request.Passward))
					return null;

				using (var redis = RedisMgr.Instance.GetClient ())
				{
					string checkPassward = redis.GetValueFromHash ("User", request.Name);
					if (string.IsNullOrEmpty (checkPassward)) {
						redis.SetEntryInHash ("User", request.Name, request.Passward);

						player = new Player ();
						player.Id = request.Name;
						player.Name = request.Name;
						player.Points = 2000;
						player.IsVip = false;

						IRedisTypedClient<Player> redisT = redis.As<Player> ();
						redisT.StoreAsHash (player);
					} else {
						if (!checkPassward.Equals (request.Passward))
							return null;

						IRedisTypedClient<Player> redisT = redis.As<Player> ();
						player = redisT.GetFromHash (request.Name);
					}
				}

				Console.WriteLine(DateTime.Now.ToString() + "$$" + string.Format("[{0}]登录成功！", request.Name));
			}
			catch (Exception e) {
				Log.WriteLog (e.Message);
			}

			return player;
		}

		public Player Post(CreaeteRqt request)
		{
			try {
				Player player = new Player ();
				player.Name = request.Name;
				player.Points = 2000;
				player.IsVip = false;

				return player;
			}
			catch (Exception e) {
				Log.WriteLog (e.Message);
				return null;
			}
		}

		//================================= Nine Blocks =================================
		public const String Z_NB_EASY_RANKING = "NineBlocks:EasyRanking";
		public const String Z_NB_NORMAIL_RANKING = "NineBlocks:NormalRanking";
		public const String Z_NB_HARD_RANKING = "NineBlocks:HardRanking";

		public PlayerRecordNB Get(NBGetRecordRqt request)
		{
			PlayerRecordNB record = null;

			try {
				string strRanking = Z_NB_NORMAIL_RANKING;
	//			if (request.Diffcult == 0)
	//				strRanking = Z_NB_EASY_RANKING;
	//			else if (request.Diffcult == 2)
	//				strRanking = Z_NB_HARD_RANKING;

				record = new PlayerRecordNB ();
				record.TopRank = 0;
				record.TopScore = 0;
				record.TopGameScore = 0;

				using (var redis = RedisMgr.Instance.GetClient ())
				{
					if (redis.GetSortedSetCount (strRanking) > 0) {
						IDictionary<string, double> topScores = redis.GetRangeWithScoresFromSortedSet (strRanking, 0, 0);
						foreach (var info in topScores)
						{
							record.TopGameScore = (int)info.Value;
							break;
						}
					}

					if (redis.SortedSetContainsItem (strRanking, request.PlayerId)) {
						double score = redis.GetItemScoreInSortedSet (strRanking, request.PlayerId);
						long rank = redis.GetItemIndexInSortedSet (strRanking, request.PlayerId);

						record.TopRank = (int)rank;
						record.TopScore = (int)score;
					}
				}
			}
			catch (Exception e) {
				Log.WriteLog (e.Message);
			}

			return record;
		}

		public PlayerRecordNB POST(NBUpdateRecordRqt request)
		{
			PlayerRecordNB record = null;

			try {
				string strRanking = Z_NB_NORMAIL_RANKING;
	//			if (request.Diffcult == 0)
	//				strRanking = Z_NB_EASY_RANKING;
	//			else if (request.Diffcult == 2)
	//				strRanking = Z_NB_HARD_RANKING;

				record = new PlayerRecordNB ();
				record.TopRank = 0;
				record.TopScore = request.NewScore;
				record.TopGameScore = 0;

				using (var redis = RedisMgr.Instance.GetClient ())
				{
					if (redis.SortedSetContainsItem (strRanking, request.PlayerId)) {
						record.TopScore = (int)redis.GetItemScoreInSortedSet (strRanking, request.PlayerId);

						if (request.NewScore < record.TopScore) {
							if (redis.AddItemToSortedSet (strRanking, request.PlayerId, request.NewScore)) {
								record.TopScore = request.NewScore;
							}
						}
					} else {
						if (redis.AddItemToSortedSet (strRanking, request.PlayerId, request.NewScore)) {
							record.TopScore = request.NewScore;
						}
					}

					record.TopRank = (int)redis.GetItemIndexInSortedSet (strRanking, request.PlayerId);

					if (record.TopRank != 0) {
						IDictionary<string, double> topScores = redis.GetRangeWithScoresFromSortedSet (strRanking, 0, 0);
						foreach (var info in topScores) {
							record.TopGameScore = (int)info.Value;
							break;
						}
					} else {
						record.TopGameScore = record.TopScore;
					}

					// level diffcult
					string playerKey = RedisMgr.Instance.MakeHashId("Player", request.PlayerId);
					int curDiffcult = 0;
					string diffcult = redis.GetValueFromHash (playerKey, "NBDiffcult");
					if (!string.IsNullOrEmpty (diffcult))
						curDiffcult = int.Parse (diffcult);

					if (request.Diffcult == curDiffcult && request.NewScore == 1) {
						curDiffcult = curDiffcult + 1;
						redis.SetEntryInHash (playerKey, "NBDiffcult", curDiffcult.ToString ());
					}

					record.TopLevel = curDiffcult;
				}
			}
			catch (Exception e) {
				Log.WriteLog (e.Message);
			}

			return record;
		}
	}
}

